using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MacomberMap.Common.User_Interfaces.Components;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using MacomberMap.Common.User_Interfaces.One_Lines.Elements;
using MacomberMap.Common.Components;
using MacomberMap.Common.Types;
using MacomberMap.Common.Database_Interface;

namespace MacomberMap.Common.User_Interfaces.One_Lines.Elements
{
    /// <summary>
    /// This class provides a viewable Unit
    /// </summary>
    public class MM_OneLine_Unit : MM_OneLine_Element
    {
        #region Variable declarations
        /// <summary>
        /// The current level of generation (relative to its limit)
        /// </summary>
        [Category("Display"), Description("The current level of generation (relative to its limit)")]
        public float GenerationLevel
        {
            get { return _GenerationLevel; }
            set { _GenerationLevel = value; }
        }
        private float _GenerationLevel;
        #endregion


        #region Initialization
        /// <summary>
        /// Initialize a new unit
        /// </summary>
        /// <param name="DisplayParameters"></param>
        public MM_OneLine_Unit(MM_OneLine_Display DisplayParameters)
            : base(DisplayParameters)
        {
        }

        /// <summary>
        /// Initialize a new viewable Unit
        /// </summary>
        /// <param name="ElemConfig">The Xml configuration for the Unit</param>
        /// <param name="DisplayParameters">The display parameters for the Unit</param>
        /// <param name="DbConnection">The database connection</param>
        public MM_OneLine_Unit(XmlElement ElemConfig, MM_OneLine_Display DisplayParameters, MM_Database DbConnection)
            : base(ElemConfig, DisplayParameters, DbConnection)
        {
            //Retrieve our arrow direction from the 
            Orientation = ParseOrientation(ElemConfig.Attributes["Orientation"].Value);

            
        }

        /// <summary>
        /// Initialize a new viewable unit
        /// </summary>
        /// <param name="SelectedElement">The CIM element upon which the unit is based</param>
        /// <param name="DisplayParameters">The display parameters</param>
        /// <param name="Orientation">The orientation of the unit</param>
        public MM_OneLine_Unit(MM_Element SelectedElement, MM_OneLine_Display DisplayParameters, MM_OneLine_Element.enumOrientations Orientation)
            : base(SelectedElement, DisplayParameters, Orientation)
        {           
        }
        #endregion


        #region Rendering
        /// <summary>
        /// Draw the unit
        /// </summary>
        /// <param name="g"></param>
        public override void DrawImage(Graphics g)
        {
            Pen ThisPen = KVLevel == null ? Pens.Red : KVLevel.Energized.ForePen;
            Point Center = CenterRect(DisplayRectangle);
            using (Brush BackBrush = new SolidBrush(BackColor))
            if (Orientation == enumOrientations.Up || Orientation == enumOrientations.Vertical)
            {
                g.DrawLine(ThisPen, Center.X - 3,Height-4, Center.X + 3,  Height-4);
                g.DrawLine(ThisPen, Center.X,Center.Y, Center.X, Height-1);
                g.FillPie(BackBrush, 0, 1-Center.Y, Width-1, Height-1, 0, 180);
                g.DrawLine(ThisPen, 0, 0, Width - 1, 0);
                g.DrawArc(ThisPen, 0, 1-Center.Y, Width - 1, Height - 1, 0, 180);
                g.FillPie(ThisPen.Brush, 0, 1-Center.Y, Width - 1, Height - 1, 0, 180f * GenerationLevel);
            }
            else if (Orientation == enumOrientations.Down)
            {
                    g.DrawLine(ThisPen, Center.X - 3, 3, Center.X + 3, 3);
                    g.DrawLine(ThisPen, Center.X, 0, Center.X, Center.Y);
                    g.FillPie(BackBrush, 0, Center.Y, Width - 1, Height - 1, 180, 180);
                    g.DrawLine(ThisPen, 0, Height-1, Width - 1, Height-1);
                    g.DrawArc(ThisPen, 0, Center.Y, Width - 1, Height - 1, 180, 180);
                    g.FillPie(ThisPen.Brush, 0, Center.Y, Width - 1, Height - 1, 180, 180f * GenerationLevel);
                
            }
            else if (Orientation == enumOrientations.Left || Orientation == enumOrientations.Horizontal || Orientation == enumOrientations.Unknown)
            {
                g.DrawLine(ThisPen, Width - 4, Center.Y - 3, Width - 4, Center.Y + 3);
                g.DrawLine(ThisPen, Center.X + 1, Center.Y, Width - 1, Center.Y);
                g.FillPie(BackBrush, 1 - Center.X, 0, Width - 1, Height - 1, 270, 180);
                g.DrawLine(ThisPen, 0, 0, 0, Height - 1);
                g.DrawArc(ThisPen, 1 - Center.X, 0, Width - 1, Height - 1, 270, 180);
                g.FillPie(ThisPen.Brush, 1 - Center.X, 0, Width - 1, Height - 1, 270, 180f * GenerationLevel);
            }
            else if (Orientation == enumOrientations.Right)
            {
                g.DrawLine(ThisPen, 3,Center.Y - 3,  3, Center.Y + 3);
                g.DrawLine(ThisPen, 0, Center.Y, Center.X, Center.Y);
                g.FillPie(BackBrush, Center.X, 0, Width - 1, Height - 1, 90, 180);
                g.DrawLine(ThisPen, Width - 1, 0, Width - 1, Height - 1);
                g.DrawArc(ThisPen, Center.X, 0, Width - 1, Height - 1, 90, 180);
                g.FillPie(ThisPen.Brush, Center.X, 0, Width - 1, Height - 1, 90, 180f * GenerationLevel);
            }
   
        }

        /// <summary>
        /// Draw the Unit text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextLabel_Paint(object sender, PaintEventArgs e)
        {
            if (DisplayParameters.Names)
                e.Graphics.DrawString(this.Name, SystemFonts.CaptionFont, Brushes.Aqua, PointF.Empty);
        }
        #endregion
    }
}


